package com.quarkus.cloud.service;

import java.util.HashMap;
import java.util.Map;

import com.quarkus.cloud.domain.entity.User;
import com.quarkus.cloud.repository.UserRepository;

import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.quarkus.panache.common.Page;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class UserService {

    @Inject
    UserRepository userRepository;

    @WithTransaction
    public Uni<User> findById(Long id){
        return userRepository.findById(id);
    }

    public Page page(String name,Integer pageNo,Integer pageSize){
    	Map<String, Object> params = new HashMap<>();
        return userRepository.page(params,Page.of(pageNo, pageSize));
    }

    @WithTransaction
    public Uni<User> insert(User user){
        return userRepository.persist(user);
    }
    @WithTransaction
    public Uni<User> update(User user){
        return userRepository.persistAndFlush(user);
    }
    @WithTransaction
    public Uni<Boolean> delete(Long id){
        return userRepository.deleteById(id);
    }

}


